use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
    fn name(&self) -> &str {
        "m20240806_000001_create_user_active_token_table"
    }
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    // Define how to apply this migration: Create the table.
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(UserActiveToken::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(UserActiveToken::Id)
                            .big_integer()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::AccountGroupId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::Username)
                            .string_len(50)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::IpAddress)
                            .string()
                            .not_null(),
                    )
                    .col(ColumnDef::new(UserActiveToken::DeviceId).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::DeviceBrand).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::DeviceModel).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::DeviceProductName).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::DeviceSystemName).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::DeviceSystemVersion).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::OperatingSystem).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::OperatingSystemVersion).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::PlatformVersion).string_len(100))
                    .col(ColumnDef::new(UserActiveToken::AppVersion).string_len(100))
                    .col(
                        ColumnDef::new(UserActiveToken::AccessToken)
                            .string_len(3000)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::Expiration)
                            .big_unsigned()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::DateGenerated)
                            .timestamp()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::DateExpired)
                            .timestamp()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::RefreshToken)
                            .string_len(3000)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::Enabled)
                            .boolean()
                            .default(true)
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::DateCreated)
                            .timestamp()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(UserActiveToken::LastUpdated)
                            .timestamp()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .to_owned(),
            )
            .await
    }

    // Define how to rollback this migration: Drop the table.
    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(UserActiveToken::Table).to_owned())
            .await
    }
}

#[derive(Iden)]
pub enum UserActiveToken {
    Table,
    Id,
    AccountGroupId,
    Username,
    IpAddress,
    DeviceId,
    DeviceBrand,
    DeviceModel,
    DeviceProductName,
    DeviceSystemName,
    DeviceSystemVersion,
    OperatingSystem,
    OperatingSystemVersion,
    PlatformVersion,
    AppVersion,
    AccessToken,
    Expiration,
    DateGenerated,
    DateExpired,
    RefreshToken,
    Enabled,
    DateCreated,
    LastUpdated,
}
